import Vue from "vue";
import VueRouter from "vue-router";
import { isLogined } from "../utils/auth";

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "/",
      name: "Home",
      redirect: "/home",
      meta: {
        showTab: true,
      },
    },
    {
      path: "/home",
      name: "Home",
      component: () => import("../pages/home/index.vue"),
      meta: {
        showTab: true,
      },
    },
    {
      path: "/user",
      name: "User",
      component: () => import("../pages/user/index.vue"),
      meta: {
        showTab: true,
        needLogin: true,
      },
    },
    {
      path: "/fenlei",
      name: "Fenlei",
      component: () => import("../pages/fenlei/index.vue"),
      meta: {
        showTab: true,
      },
    },
    {
      path: "/cart",
      name: "Cart",
      component: () => import("../pages/cart/index.vue"),
      meta: {
        showTab: true,
      },
    },
    {
      path: "/find",
      name: "Find",
      component: () => import("../pages/find/index.vue"),
      meta: {
        showTab: true,
      },
    },
    {
      path: "/register",
      name: "Register",
      component: () => import("../pages/register/index.vue"),
      meta: {
        showTab: false,
      },
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("../pages/login/index.vue"),
      meta: {
        showTab: false,
      },
    },

    {
      path: "/xg",
      name: "Xg",
      component: () => import("../pages/xg/index.vue"),
    },
    {
      path: "/detail/:id",
      name: "Detail",
      component: () => import("../pages/detail/index.vue"),
      meta: {
        showTab: false,
      },
    },
    {
      path: "/order",
      name: "Order",
      component: () => import("../pages/order/index.vue"),
      meta: {
        showTab: false,
      },
    },
    {
      path: "/addressEdit",
      name: "AddressEdit",
      component: () => import("../pages/addressEdit/index.vue"),
      meta: {
        showTab: false,
      },
    },
  ],
});
//登录判断
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogined()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});

export default router;
